/**
 * pages模版快速生成脚本,执行命令 npm run tep `文件名`
 */

const fs = require('fs');

const dirName = process.argv[2];
const curDate = new Date().toLocaleString().split(' ')[0] + ' ' + new Date().toLocaleString().split(' ')[1];

if (!dirName) {
    console.log('文件夹名称不能为空！');
    console.log('示例：yarn tep test');
    process.exit(0);
}

// 页面模版
const indexTep = `// createTime: ${curDate}';
import React, { useEffect } from 'react'
import styles from './index.less'

const ${titleCase(dirName)} = () => {
    useEffect(() => {}, []);
    return (
        <div className={styles.${dirName}Page}>
            ${dirName}
        </div>
    );
}

export default ${titleCase(dirName)}
`;

// less文件模版
const lessTep = `// createTime: ${curDate}`;

fs.mkdirSync(`./src/components/${titleCase(dirName)}`); // mkdir $1
process.chdir(`./src/components/${titleCase(dirName)}`); // cd $1

fs.writeFileSync('index.tsx', indexTep);
fs.writeFileSync('index.less', lessTep);

// eslint-disable-next-line no-console
console.log(`组件${dirName}已创建`);

function titleCase(str) {
    const array = str.toLowerCase().split(' ');
    for (let i = 0; i < array.length; i++) {
        array[i] = array[i][0].toUpperCase() + array[i].substring(1, array[i].length);
    }
    const string = array.join(' ');
    return string;
}

process.exit(0);
